package com.tgl.vegetables.plug.meitu;

import android.accessibilityservice.AccessibilityService;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import androidx.annotation.NonNull;

import com.tgl.vegetables.plug.service.BaseAccessibilityService;
import com.tgl.vegetables.plug.service.IAccessibilityService;

import java.util.ArrayList;
import java.util.List;

public class MeituAccessibilityService implements IAccessibilityService {

    private static final int VEGETABLES = 1;
    private static final int PREFERRED = 2;

    private int mCurrentType = VEGETABLES;

    private MeituVegetablesAccessibilityService vegetablesService = null;
    private MeituPreferredAccessibilityService preferredService = null;

    private BaseAccessibilityService mCurrentService = null;

    public MeituAccessibilityService(){
        vegetablesService = new MeituVegetablesAccessibilityService();
        preferredService = new MeituPreferredAccessibilityService();
    }

    private BaseAccessibilityService getService() {
        switch (mCurrentType) {
            case VEGETABLES:
                return vegetablesService;
            case PREFERRED:
                return preferredService;
            default:
                return vegetablesService;
        }
    }

    @Override
    public void onAccessibilityEvent(AccessibilityService context, AccessibilityEvent accessibilityEvent) {
        int type = accessibilityEvent.getEventType();
        String className = accessibilityEvent.getClassName().toString();
        if (type == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
            switch (className) {
                case "com.meituan.retail.c.android.newhome.newmain.NewMainActivity":
                    preferredService.onInterrupt();
                    mCurrentType = VEGETABLES;
                    break;
                case "com.meituan.mmp.lib.mp.MPActivity0":
                    vegetablesService.onInterrupt();
                    mCurrentType = PREFERRED;
                    break;
                default:
                    break;
            }
        }
        getService().onAccessibilityEvent(context, accessibilityEvent);
    }

    @Override
    public void onInterrupt() {
        getService().onInterrupt();
    }
}
